package com.clw.service.impl;


import com.clw.dataobject.Category;
import com.clw.repository.CategoryRepository;
import com.clw.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/10/9.
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryRepository repository;
    @Override
    public List<Category> findAll() {
        return repository.findAll();
    }

    @Override
    public Category findById(Integer id) {
        return repository.findOne(id);
    }

    @Override
    public Category save(Category category) {
        return repository.save(category);
    }

    @Override
    public Category getTopId(Category category) {
        if (category.getPid()!=0){
            Category parentCategory=findById(category.getPid());
            return getTopId(parentCategory);
        }
        else {
            return category;
        }
    }

    @Override
    public void del(Category category) {
        repository.delete(category);
    }

    @Override
    public Long getChildrenCount(Integer id) {
        return repository.countByPid(id);
    }


}
